package com.engr.auth;

import com.engr.dto.user.MemberDetails;
import com.engr.domain.Member;
import com.engr.repository.member.MemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class CustomUserDetailService implements UserDetailsService {

	@Autowired
	private MemberRepository memberDao;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		Member member = memberDao.findByUsername(username)
				.orElseThrow(() -> new UsernameNotFoundException("Could not find user " + username));
		return MemberDetails.create(member);
	}

}
